package com.spring.learn;

import org.springframework.util.*;
import org.springframework.util.xml.DomUtils;
import org.w3c.dom.Element;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;

/**
 * Created by baiguantao on 2017/4/21.
 */
public class SpringUtilsTest {
    /**
     * 测试Spring的StringUtils
     * 启动提供了比较，替换，包含
     */
    public static void StringUtils(String txt){
        System.out.println(StringUtils.isEmpty(txt));
        System.out.println(StringUtils.containsWhitespace(txt));
    }
    /**
     * 测试Spring的NumberUtils
     * 启动提供了比较，替换，包含
     */
    public static void StreamUtils() throws Exception {
        InputStream input = Class.forName(SpringUtilsTest.class.getName()).getResourceAsStream("/streamutils.txt");
        String result=StreamUtils.copyToString(input,Charset.forName("UTF-8"));
        System.out.println(result);
    }

    /**
     * ResourceUtils工具
     * @throws Exception
     */
    public static void ResourceUtils() throws Exception {
        File file= ResourceUtils.getFile("classpath:streamutils.txt");
        String result=StreamUtils.copyToString(new FileInputStream(file),Charset.forName("UTF-8"));
        System.out.println(result);
    }/**
     * FileSystemUtils
     * 可以递归复制和删除文件
     * @throws Exception
     */
    public static boolean FileSystemUtils() throws Exception {
        boolean flag= FileSystemUtils.deleteRecursively(new File("d:\\test"));
        return  flag;
    }
    /**
     * FileCopyUtils
     * 只针对文件 不包含目录
     * @throws Exception
     */
    public static void FileCopyUtils() throws Exception {
        FileCopyUtils.copy(new File("d:\\t\\1\\1.txt"),new File("d:\\t\\2\\2.txt"));
    }
    /**
     * DigestUtils
     * 加密工具类
     * @throws Exception
     */
    public static void DigestUtils() throws Exception {
        System.out.println(DigestUtils.md5DigestAsHex("123456".getBytes()));
    }
    /**
     * CollectionUtils
     * 集合工具类
     * 判断是否包含  是否空等
     * @throws Exception
     */
    public static void CollectionUtils() throws Exception {
        System.out.println(CollectionUtils.isEmpty(new ArrayList<Object>()));
    }
/**
     * DomUtils
     * 集合工具类
     * 判断是否包含  是否空等
     * @throws Exception
     */
    public static void DomUtils() throws Exception {
        String string="";
        Element element=null;
        DomUtils.getChildElementByTagName(element,"name");
        System.out.println(CollectionUtils.isEmpty(new ArrayList<Object>()));
    }

    public static void main(String[] args) throws Exception {
        //SpringUtilsTest.StringUtils("1  22 444 ");
        //SpringUtilsTest.StreamUtils();
        //jdk的uuid生成器
        System.out.println(new JdkIdGenerator().generateId());
        //SpringUtilsTest.FileCopyUtils();
        //SpringUtilsTest.DigestUtils();
        SpringUtilsTest.CollectionUtils();

    }
}
